using RotationUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RotationChart
{
    public partial class Demonstration : Form
    {
        AllPointAndSize source;
        public Demonstration()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲

        }
        Bitmap SM;
        private void button1_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            source = new AllPointAndSize();
            source.AddBitmapData(@"D:\richFile\down\狐狸\TIM图片20181021093249.jpg");
            source.AddBitmapData(@"D:\richFile\down\狐狸\TIM图片20181024162459.jpg");
            source.AddBitmapData(@"D:\richFile\down\狐狸\TIM图片20181031130620.jpg");
            source.AddBitmapData(@"D:\richFile\down\狐狸\TIM图片20181123104543.jpg");
            source.AddBitmapData(@"D:\richFile\down\狐狸\TIM图片20181124224354.jpg");
            source.Init(5, 20, this.Width, this.Height, this.Width * 0.6, this.Height * 0.6, this.Width * 0.4, this.Height * 0.4, 1000, 20);
            source.FrameRefresh += Source_FrameRefresh;
            source.Run();
            // DramLine();
        }
        private Color getColor()
        {
            int iSeed = 10;
            Random ro = new Random(10);
            long tick = DateTime.Now.Ticks;
            Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));

            int R = ran.Next(255);
            int G = ran.Next(255);
            int B = ran.Next(255);
            B = (R + G > 400) ? R + G - 400 : B;//0 : 380 - R - G;
            B = (B > 255) ? 255 : B;
            return Color.FromArgb(R, G, B);
        }
        private void DramLine()
        {
            var date = source.GetPoint();
            var g = this.CreateGraphics();
            Pen pen = new Pen(Color.Red);
            double maxy = 0, miny = 0;
            for (int i = 0; i < date.Length; i++)
            {
                if (miny == 0)
                {
                    miny = date[i].Point.Y;
                }
                if (date[i].Point.Y > maxy)
                {
                    maxy = date[i].Point.Y;
                }
                if (date[i].Point.Y < miny)
                {
                    miny = date[i].Point.Y;
                }
                if (i + 1 < date.Length)
                {
                    pen.Color = getColor();
                    g.DrawLine(pen, (float)date[i].Point.X, (float)date[i].Point.Y, (float)date[i + 1].Point.X, (float)date[i + 1].Point.Y);
                }
                Console.WriteLine(i + "  " + date[i].Point.X + "  " + date[i].Point.Y);
            }
            for (int i = 0; i < date.Length; i++)
            {
                Console.WriteLine(i + "  " + date[i].Size.Width + "  " + date[i].Size.Height);
                if (i + 1 < date.Length)
                {
                    g.DrawLine(pen, i + 10, (float)date[i].Size.Width, i + 11, (float)date[i + 1].Size.Width);
                }
            }
            for (int i = 0; i < date.Length; i++)
            {
                if (i + 1 < date.Length)
                {
                    g.DrawLine(pen, i + 10, (float)date[i].Size.Height, i + 11, (float)date[i + 1].Size.Height);
                }
            }
        }
        private void Source_FrameRefresh(Bitmap obj)
        {
            var G = Graphics.FromImage(SM);
            G.DrawImage(obj, 0, 0);
            AutoRefresh();
            //  var g = this.CreateGraphics();
            ////  g.Clear(Color.Black);
            //  g.DrawImage(obj, 0, 0);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (SM != null)
            {
                e.Graphics.DrawImage(SM, 0, 0);
            }
        }
        private void AutoRefresh()
        {
            if (this.IsHandleCreated)
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new Action(AutoRefresh));
                }
                else
                {
                    this.Refresh();
                }
            }
        }

        private void Demonstration_Load(object sender, EventArgs e)
        {
            SM = new Bitmap(this.Width, this.Height);
        }
    }
}
